package com.iflysse.services.impl;

import com.iflysse.common.ResultEnum;
import com.iflysse.dao.CourseLabelDao;
import com.iflysse.dao.LabelDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.AddLabelBoReq;
import com.iflysse.pojo.bo.req.DeleteLabelBoReq;
import com.iflysse.pojo.bo.req.LabelBoReq;
import com.iflysse.pojo.bo.resp.LabelAllBoResp;
import com.iflysse.pojo.bo.resp.LabelBoResp;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.AddLabelPoReq;
import com.iflysse.pojo.po.req.DeleteLabelPoReq;
import com.iflysse.pojo.po.req.LabelPoReq;
import com.iflysse.pojo.po.resp.CourseLabelPoResp;
import com.iflysse.pojo.po.resp.LabelAllPoResp;
import com.iflysse.pojo.po.resp.LabelPoResp;
import com.iflysse.services.LabelService;
import com.iflysse.util.CommonUtil;
import lombok.AllArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    LabelDao labelDao;

    @Autowired
    CourseLabelDao courseLabelDao;
    /**
     * 添加标签
     *
     * @param addLabelBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp addLabel(AddLabelBoReq addLabelBoReq) throws SQLException {
        //service对象转dao对象
        AddLabelPoReq addLabelPoReq = CommonUtil.objToObj(addLabelBoReq, AddLabelPoReq.class);
        LabelAllPoResp labelsByTitle = new LabelAllPoResp();
        List<LabelPoResp> labelsByTitle1 = labelDao.getLabelsByTitle(addLabelPoReq);
            labelsByTitle.setLabelAllPoResp(labelsByTitle1);
        //判断是否存在该名称title,若不存在添加,存在返回已经存在
        if(labelsByTitle.getLabelAllPoResp().size() == 0) {
            CommonPoResp commonPoResp = new CommonPoResp();
            //执行sql添加
            int i = labelDao.addLabel(addLabelPoReq);
            CommonUtil.printToPoResult(commonPoResp,i);
            CommonBoResp commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
            return commonBoResp;
        }else{
            CommonBoResp commonBoResp = new CommonBoResp();
            commonBoResp.setCode(ResultEnum.HASEXISTED.getCode());
            commonBoResp.setMsg("标签名称"+ ResultEnum.HASEXISTED.getMsg());
            return commonBoResp;
        }

    }

    /**
     * 修改标签
     *
     * @param labelBoReq
     * @return
     */
    @Override
    public CommonBoResp updateLabel(LabelBoReq labelBoReq) throws SQLException {
        LabelPoReq labelPoReq = CommonUtil.objToObj(labelBoReq, LabelPoReq.class);

        List<CourseLabelPoResp>list=courseLabelDao.queryCourseLabelByLabelId(labelPoReq.getLabelId());
log.info("修改标签时该标签与课程绑定数量："+list);
        if(list.size()>0){
            CommonBoResp commonBoResp=new CommonBoResp();
            commonBoResp.setCode(ResultEnum.LABELNOTUPDATE.getCode());
            commonBoResp.setMsg(ResultEnum.LABELNOTUPDATE.getMsg());
            log.info("修改标签时业务层返回结果："+commonBoResp);
            return commonBoResp;
        }
        CommonPoResp commonPoResp = new CommonPoResp();
        int i = labelDao.updateLabel(labelPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        CommonBoResp commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);

        return commonBoResp;
    }

    /**
     * 删除标签
     *
     * @param deleteLabelBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp deleteLabel(DeleteLabelBoReq deleteLabelBoReq) throws SQLException {
        DeleteLabelPoReq deleteLabelPoReq = CommonUtil.objToObj(deleteLabelBoReq, DeleteLabelPoReq.class);

        List<CourseLabelPoResp>list=courseLabelDao.queryCourseLabelByLabelId(deleteLabelPoReq.getLabelId());
        log.info("删除标签时该标签与课程绑定的结果："+list);
        if(list.size()>0){
            CommonBoResp commonBoResp=new CommonBoResp();
            commonBoResp.setCode(ResultEnum.LABELNOTUPDATE.getCode());
            commonBoResp.setMsg(ResultEnum.LABELNOTUPDATE.getMsg());
            log.info("删除标签时业务层返回结果："+commonBoResp);
            return commonBoResp;
        }
        CommonPoResp commonPoResp = new CommonPoResp();
        int i = labelDao.deleteLabel(deleteLabelPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        CommonBoResp commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        return commonBoResp;
    }

    /**
     * 获取所有的标签
     *
     * @return 标签
     */
    @Override
    public LabelAllBoResp getAllLabels() throws SQLException {
        LabelAllPoResp allLabelsPO = new LabelAllPoResp();

        List<LabelPoResp> allLabels = labelDao.getAllLabels();
        allLabelsPO.setLabelAllPoResp(allLabels);
        LabelAllBoResp labelAllBoResp = new LabelAllBoResp();
        List<LabelBoResp> labelBoRespList = new ArrayList<>();
        allLabelsPO.getLabelAllPoResp().forEach(labelPoResp -> {
            LabelBoResp labelBoResp  = new LabelBoResp();
            labelBoResp.setLabelId(labelPoResp.getLabelId());
            labelBoResp.setLabelTitle(labelPoResp.getLabelTitle());
            labelBoRespList.add(labelBoResp);
    });
        labelAllBoResp.setLabelAllBoResp(labelBoRespList);
        return labelAllBoResp;
    }
}
